/**
 * 
 */
package br.com.mb;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.faces.bean.ManagedBean;

/**
 * @author Joelson
 *
 */
@ManagedBean(name = "fileDownloadBean")
public class FileDownloadBean {
	
	private static final int DEFAULT_BUFFER_SIZE = 10240;
	private String filePath = "c:\\imagens\\documento.pdf";

	public void downLoad() throws IOException {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
		// Obtem o caminho para o arquivo e efetua a leitura
		File file = new File(filePath);
		if (!file.exists()) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		response.reset();
		response.setBufferSize(DEFAULT_BUFFER_SIZE);
		// isso faz abrir a janelinha de download
		response.setContentType("application/octet-stream");		
		//response.setContentType("application/download");		
		
		response.setHeader("Content-Length", String.valueOf(file.length()));
		response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
		BufferedInputStream input = null;
		BufferedOutputStream output = null;
		try {
			input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
			output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
			byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
			int length;
			while ((length = input.read(buffer)) > 0) {
				output.write(buffer, 0, length);
			}
		} finally {
			input.close();
			output.close();
		}
		context.responseComplete();
	}
}
